<!--
 * @Author: MarsLay
 * @Date: 2021-12-23 09:39:56
 * @LastEditTime: 2023-12-25 10:45:27
 * @LastEditors: MarsLay 331412466@qq.com
 * @Description: 上传组件，支持图片/视频
 * @FilePath: \smart-sports-managee:\project\autopilot\App\components\upload\upload.vue
-->
<template>
  <view class="upload">
    <view class="multi-label single-ellipsis">
      <view class="label-title">{{ label }}</view>
      <view v-if="type == 'image' && !limit" class="num">({{ num }}/{{ count }})</view>
    </view>
    <view class="part">
      <htz-image-upload v-if="type == 'image'" :sourceType="['camera', 'album']" :chooseNum="count" :quality="quality"
        :max="max" :uploadSuccess="success" :action="`${api}/tools/upload`" :add="!disabled" :remove="!disabled"
        v-model="val">
      </htz-image-upload>
      <htz-image-upload v-else :max="max" :action="`${api}/tools/upload`" :chooseNum="count" :uploadSuccess="success"
        :add="!disabled" :remove="!disabled" name="file" v-model="val" mediaType="video">
      </htz-image-upload>
    </view>
  </view>
</template>

<script>
import htzImageUpload from "@/components/upload/htz-image-upload.vue";

export default {
  name: "upload",
  components: {
    htzImageUpload,
  },
  props: {
    label: {
      type: String,
      default: "",
    },
    mark: {
      type: String,
      default: "",
    },
    type: {
      type: String,
      default: "",
    },
    mark: {
      type: String,
      default: "",
    },
    value: {
      type: Array,
      default: () => [],
    },
    api: {
      type: String,
      default: "",
    },
    count: {
      type: Number,
      default: 3,
    },
    max: {
      type: Number,
      default: 20,
    },
    quality: {
      type: Number,
      default: 80,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    limit: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      val: this.value,
    };
  },
  computed: {
    num() {
      return this.val.length;
    },
  },
  watch: {
    val(v) {
      this.$emit('change', { value: v, key: this.mark });
    },
  },
  onLoad(d) { },
  onShow() { },
  methods: {
    success(res) {
      let _res = JSON.parse(res.data);
      if (_res.code == 200) {
        //#ifdef APP-PLUS
        plus.io.resolveLocalFileSystemURL(
          "_doc/",
          (entry) => {
            entry.removeRecursively(
              (entry) => {
                console.log("删除成功");
              },
              (err) => {
                console.log("删除失败");
              }
            );
          },
          (err) => {
            console.log("没找到对象");
          }
        );
        //#endif
        return {
          success: true,
          url: _res.data,
        };
      } else {
        return {
          success: false,
          url: "",
        };
      }
    },
  },
};
</script>
<style lang='less' scoped>
.upload {
  padding: 30upx 40upx 10upx 40upx;
  background-color: #ffffff;
}

.multi-label {
  .flex(row, flex-start);
  margin-bottom: 20upx;
  color: #888990;
}
</style>